package com.yetm.springbootdemo.utils;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class CodeUtils {

    public static final String[] PATCH = {"00000", "0000", "000", "00", "0", ""};
    public static final int ENCRYPTION = 2022666;

    /**
     * 生成6位验证码
     * @param phone
     * @return
     */
    public String generateCode(String phone) {
        int hashCode = phone.hashCode();
        int encryptionHash = hashCode ^ ENCRYPTION;
        long currentTimeMillis = System.currentTimeMillis();
        long encryptionCode = encryptionHash ^ currentTimeMillis;
        encryptionCode = Math.abs(encryptionCode % 1000000);
        String codeStr = encryptionCode + "";
        int length = codeStr.length();
        codeStr = PATCH[length-1] + codeStr;
        return codeStr;
    }

    @Cacheable(value = "smsCache", key = "#phone")
    public String getCacheCode(String phone) {
        return null;
    }

}
